import { createRouter, createWebHistory } from '@ionic/vue-router';
import { RouteRecordRaw } from 'vue-router';
import { useUserStore } from '@/stores/user';
import HomePage from '../views/HomePage.vue'
import LoginPage from '../views/LoginPage.vue'
import RegisterPage from '../views/RegisterPage.vue'
import RoleSelectPage from '../views/RoleSelectPage.vue'
import LandlordVerificationPage from '../views/LandlordVerificationPage.vue'
import ServiceAgreementPage from '../views/ServiceAgreementPage.vue'
import QRCodePage from '../views/QRCodePage.vue'
import MessageListPage from '../views/MessageListPage.vue'
import TabsLayout from '../views/TabsLayout.vue'
import ProfilePage from '../views/ProfilePage.vue'
import ActivityPage from '../views/ActivityPage.vue'
import ActivityRulesPage from '../views/ActivityRulesPage.vue'
import OrganizationPage from '../views/OrganizationPage.vue'
import MyPropertiesPage from '../views/MyPropertiesPage.vue'
import PublishPropertyPage from '../views/PublishPropertyPage.vue'
import DiscoverPage from '../views/DiscoverPage.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: LoginPage,
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: RegisterPage,
    meta: { requiresAuth: false }
  },
  {
    path: '/role-select',
    name: 'RoleSelect',
    component: RoleSelectPage,
    meta: { requiresAuth: true }
  },
  {
    path: '/home',
    redirect: '/tabs/home'
  },
  {
    path: '/tabs',
    component: TabsLayout,
    meta: { requiresAuth: true, requiresRole: true },
    children: [
      {
        path: '',
        redirect: '/tabs/home'
      },
      {
        path: 'home',
        name: 'Home',
        component: HomePage
      },
      {
        path: 'messages',
        name: 'Messages',
        component: MessageListPage
      },
      {
        path: 'discover',
        name: 'Discover',
        component: DiscoverPage
      },
      {
        path: 'profile',
        name: 'Profile',
        component: ProfilePage
      }
    ]
  },
  {
    path: '/landlord-verification',
    name: 'LandlordVerification',
    component: LandlordVerificationPage,
    meta: { requiresAuth: true, requiresRole: true }
  },
  {
    path: '/service-agreement',
    name: 'ServiceAgreement',
    component: ServiceAgreementPage,
    meta: { requiresAuth: false }
  },
  {
    path: '/qrcode',
    name: 'QRCode',
    component: QRCodePage,
    meta: { requiresAuth: true, requiresRole: true }
  },
  {
    path: '/activity',
    name: 'Activity',
    component: ActivityPage,
    meta: { requiresAuth: true, requiresRole: true }
  },
  {
    path: '/activity-rules',
    name: 'ActivityRules',
    component: ActivityRulesPage,
    meta: { requiresAuth: true, requiresRole: true }
  },
  {
    path: '/organization',
    name: 'Organization',
    component: OrganizationPage,
    meta: { requiresAuth: true, requiresRole: true }
  },
  {
    path: '/my-properties',
    name: 'MyProperties',
    component: MyPropertiesPage,
    meta: { requiresAuth: true, requiresRole: true }
  },
  {
    path: '/publish-property',
    name: 'PublishProperty',
    component: PublishPropertyPage,
    meta: { requiresAuth: true, requiresRole: true }
  },

]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()

  // 检查是否需要登录
  if (to.meta.requiresAuth && !userStore.isLoggedIn) {
    next('/login')
    return
  }

  // 检查是否需要角色
  if (to.meta.requiresRole && !userStore.currentRole) {
    next('/role-select')
    return
  }

  // 如果已设置角色的用户试图访问角色选择页面，重定向到主页
  if (to.path === '/role-select' && userStore.isLoggedIn && userStore.user?.hasSetRoles) {
    next('/tabs/home')
    return
  }

  // 如果已登录且访问登录或注册页，重定向到角色选择或主页
  if ((to.path === '/login' || to.path === '/register') && userStore.isLoggedIn) {
    if (userStore.currentRole) {
      next('/tabs/home')
    } else {
      next('/role-select')
    }
    return
  }

  next()
})

export default router
